﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Resources;
using System.Data.SqlClient;
using System.Data;
using ServicioWebRest.Areas.Api.Controllers;

namespace ServicioWebRest.Areas.Api.Models
{
    public class TesoreriaManager
    {
        ResourceManager rm = new ResourceManager(typeof(RecursosWeb));

        public List<Dictionary<string, object>> ObtenerCartera(string usu, int est)
        {
            // Create the command and the connection.
            string sql = "[sp_extrae_cartera] '" + usu + "',"+est;
            SqlConnection con = new SqlConnection(rm.GetString("connectionString"));
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable ds = new DataTable();
            // Fill the DataSet.
            da.Fill(ds);
            DataTableaLista(ds);
            return DataTableaLista(ds);

        }

        /***************INSERTAR CARTERA***************/
        public bool InsertarTesoreria(Tesoreria[] tes)
        {
            try
            {
                //Pedidos

                SqlConnection con = new SqlConnection(rm.GetString("connectionString"));
                SqlCommand cmd = new SqlCommand("[sp_inserta_cartera]", con);
                cmd.CommandType = CommandType.StoredProcedure;                
                foreach (Tesoreria teso in tes)
                {                     
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@ws_numero_factura", SqlDbType.Char, 100);
                    cmd.Parameters.Add("@ws_numero_documento", SqlDbType.Char, 100);
                    cmd.Parameters.Add("@ws_loc_id", SqlDbType.Char, 100);
                    cmd.Parameters.Add("@ws_usu", SqlDbType.Char, 100);
                    cmd.Parameters.Add("@ws_latitude", SqlDbType.Float);
                    cmd.Parameters.Add("@ws_longitude", SqlDbType.Float);
                    cmd.Parameters.Add("@ws_valor", SqlDbType.Float); 
                    cmd.Parameters.Add("@ws_tipo", SqlDbType.Int); 
                    cmd.Parameters.Add("@ws_fecha_pago", SqlDbType.DateTime);

                    cmd.Parameters["@ws_numero_factura"].Value = teso.tdt_numero_factura;
                    cmd.Parameters["@ws_numero_documento"].Value = teso.tdt_numero_documento;
                    cmd.Parameters["@ws_loc_id"].Value = teso.loc_id;
                    cmd.Parameters["@ws_usu"].Value = teso.usu_id;
                    cmd.Parameters["@ws_latitude"].Value = teso.latitud;
                    cmd.Parameters["@ws_longitude"].Value = teso.longitud;
                    cmd.Parameters["@ws_valor"].Value = teso.tet_valor;
                    cmd.Parameters["@ws_tipo"].Value = teso.tdt_tipo;
                    cmd.Parameters["@ws_fecha_pago"].Value = teso.tem_fecha_pago;

               
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    
                }

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /*****************************TOOL********************/
        public List<Dictionary<string, object>> DataTableaLista(DataTable dt)
        {
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new

            System.Web.Script.Serialization.JavaScriptSerializer();
            List<Dictionary<string, object>> rows =
              new List<Dictionary<string, object>>();
            Dictionary<string, object> row = null;

            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName.Trim(), dr[col]);
                }
                rows.Add(row);
            }
            return rows;
        }
    }
}